package controller

import (
	"fmt"
	"gin/response"
	"gin/service"
	"gin/types"
	"github.com/gin-gonic/gin"
)

var (
	workTimeService = service.WorkTime{}
)

type WorkTime struct {
}

// 租户成员工时timelog修正统计
func (c *WorkTime) GetTenantUserStatistics(ctx *gin.Context) {
	var req types.GetTenantUserWorkTimeStatistics
	err := ctx.ShouldBindJSON(&req)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	tenantId := service.GetInt64FromContext(ctx, "tenant_id")

	resData, queryErr := workTimeService.GetTenantUserStatistics(tenantId, req.ProjectIdList, req.WorkDateStart, req.WorkDateEnd, req.DepartmentId, req.RoleCode, req.UserIds, ctx)

	if queryErr != nil {
		// 查询失败
		response.Fail(ctx, nil, queryErr.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": resData}, "success")
	}
}

// ExportUserStatistics 导出工时报表
func (c *WorkTime) ExportUserStatistics(ctx *gin.Context) {
	var req types.ExportUserWorkTimeStatistics
	//err := ctx.ShouldBindJSON(&req)
	//
	//if err != nil {
	//	response.Fail(ctx, nil, err.Error())
	//	return
	//}

	fmt.Println(ctx.Get("tenant_id"))

	tenantId := service.GetInt64FromContext(ctx, "tenant_id")

	resData, queryErr := workTimeService.ExportStatistics(tenantId, req.Param.ProjectIdList, req.Param.WorkDateStart, req.Param.WorkDateEnd, req.Param.Page, ctx)

	if queryErr != nil {
		// 查询失败
		response.Fail(ctx, nil, queryErr.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": resData}, "success")
	}
}

func (c *WorkTime) PlanNodeReportList(ctx *gin.Context) {
	var req types.PlanNodeReports
	err := ctx.ShouldBindJSON(&req)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	fmt.Println(ctx.Get("tenant_id"))
	//tenantId := service.GetInt64FromContext(ctx, "tenant_id")

	resData, queryErr := workTimeService.GetPlanNodeReportList(req.Param.ProjectId, req.Param.ReportDate)

	if queryErr != nil {
		// 查询失败
		response.Fail(ctx, nil, queryErr.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": resData}, "success")
	}
}
